/*
 * 项目名称:    wms-server-jeecg
 * 创建人员:    FanZhiwen
 * 创建时间:    2023-10-06
 */
package org.jeecg.modules.flow.callback.inbound.purchase;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.business.doc.business.entity.WmsInReceipt;
import org.jeecg.modules.business.doc.business.entity.WmsInReceiptMaterial;
import org.jeecg.modules.business.doc.business.mapper.WmsInReceiptMapper;
import org.jeecg.modules.business.doc.business.service.IWmsInReceiptMaterialService;
import org.jeecg.modules.business.third.service.ISapOperateService;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskService;
import org.jeecg.modules.flow.callback.enums.SimpleDocumentTypeEnum;
import org.jeecg.modules.flow.callback.enums.TransferStatusEnum;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 采购入库(铁心)过账
 *
 * @author FanZhiwen
 * @version 1.0.0
 * @since 2023-10-06
 */
@Component
@Slf4j
public class R101PurchasePost<T> extends BaseCustomBeanRun<T> {
    @Resource
    private WmsInReceiptMapper wmsInReceiptMapper;
    @Autowired
    private ISapOperateService sapOperateService;

    @Resource
    private IWmsInReceiptTaskService wmsInReceiptTaskService;

    @Autowired
    private IWmsInReceiptMaterialService inReceiptMaterialService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void actureRun() {
        log.info("调拨入库101过账执行了");
        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        WmsInReceipt po = wmsInReceiptMapper.selectById(baseFlowDto.getInfoId());
        List<WmsInReceiptMaterial> list = inReceiptMaterialService.list(new LambdaQueryWrapper<WmsInReceiptMaterial>()
                .eq(WmsInReceiptMaterial::getWmsInReceiptId, po.getId())
        );
        List res = new ArrayList<>();
        try {
            res = sapOperateService.warehouseByOrder(po.getId(), list);
        }catch (Exception e){
            baseFlowDto.setResult(Result.error("过账失败: "+e.getMessage()));
            //更新过账状态 过账失败
            wmsInReceiptTaskService.updateTransferStatus(SimpleDocumentTypeEnum.IN_RECEIPT,null,po.getId(), TransferStatusEnum.TRANSFER_FAIL);
        }
        log.info("调拨入库101过账结束 响应: {}", res);
        if (CollectionUtil.isNotEmpty(res)) {
            Object o = res.get(0);
            String jsonString = JSON.toJSONString(o);
            JSONObject jsonObject = JSONObject.parseObject(jsonString);
            log.info("解析之后:{}",jsonObject);
            if ("E".equals(jsonObject.get("result"))) {
                baseFlowDto.setResult(Result.error("过账失败: " + jsonObject.get("message")));
                //更新过账状态 过账失败
                wmsInReceiptTaskService.updateTransferStatus(SimpleDocumentTypeEnum.IN_RECEIPT,null,po.getId(), TransferStatusEnum.TRANSFER_FAIL);
            }else {
                //更新过账状态 过账成功
                wmsInReceiptTaskService.updateTransferStatus(SimpleDocumentTypeEnum.IN_RECEIPT,null,po.getId(), TransferStatusEnum.TRANSFER_SUCCESS);
                log.info("调拨入库101过账结束了");
                baseFlowDto.setResult(Result.ok("操作成功"));
            }
        }
    }
}
